package com.chenchao.javafile;

import java.io.*;

/**
 * @encoding : UTF-8
 * @Time : 2020/11/15 9:39
 * @Author : Chen Chao
 * @File : javaSE.com.chenchao.javaFile.Test4.java
 * @Software : IntelliJ IDEA
 * @since : 1.8
 */
public class Test3 {
    public static void main(String[] args) throws IOException {
        test1();
        //test2();
        //test3();
        //test4();
        //test5();
        //test6();
    }

    /**
     * 文件输入流
     */
    public static void test1() throws IOException {
        File file = new File("myjava01/src/fo/aa.txt");
        FileOutputStream fileOutputStream2 = new FileOutputStream(file);
        FileOutputStream fileOutputStream3 = new FileOutputStream("myjava01/src/fo/a2.txt");
        FileOutputStream fileOutputStream = new FileOutputStream(file,true);
        /*如果文件是文件夹就报错拒绝访问，没有就新建一个*/
        /*第二个参数是是否追加写，默认false*/


        fileOutputStream.write(97);
        //int类型四个字节，只保留一个字节进入输入流

        fileOutputStream.write("\r\n".getBytes());
        //"\r\n"windows中的换行
        //String.getBytes()获取字符串的字节数组
        //使用平台的默认字符集将此String编码为字节序列，将结果存储到新的字节数组中。

        fileOutputStream.write("和VS等等v发生的v发生的".getBytes());

        fileOutputStream.write("abcdefg".getBytes(),2,3);
        //后面两个参数是从第2个位置起，写3个
        fileOutputStream.close();


    }

    /**
     * 字节输入流
     */
    public static void test2() throws IOException {
        File file = new File("myjava01/src/fo/aa.txt");
        FileInputStream fileInputStream = new FileInputStream(file);
        //如果文件不存在，直接报FileNotFoundException异常

        int read = fileInputStream.read();
        //一次只读一个字节，会保存读取位置
        System.out.println(read);
        //System.out.println((char)read);

        //可以转成char类型
        System.out.println(fileInputStream.read());
        System.out.println(fileInputStream.read());
        System.out.println(fileInputStream.read());
        //当读取到最后一个字节时，返回-1
        System.out.println(fileInputStream.read());
        fileInputStream.close();
    }

    /**
     * while读取信息
     */
    public static void test3() throws IOException {
        File file = new File("myjava01/src/fo/aa.txt");
        FileInputStream fileInputStream = new FileInputStream(file);
        int read=0;
        while ((read=fileInputStream.read())!=-1){
            System.out.println((char)read);
        }
        fileInputStream.close();
    }

    /***
     * 一次读取多个字节放入数组当中
     */
    public static void test4() throws IOException {
        File file = new File("myjava01/src/fo/aa.txt");
        byte[] bytes = new byte[2];
        FileInputStream fileInputStream = new FileInputStream(file);
        int len = fileInputStream.read(bytes);
        //返回读取个数,读取结果放到数组当中去
        System.out.println(len);
        System.out.println(new String(bytes));
        //new String(bytes)把字节数组转成字符串
        //str.getBytes() 把字符串转成字节数组
        //下一次读取会覆盖前一次读取的内容,
        // 没有读取就不覆盖，部分读取就部分覆盖

        System.out.println(new String(bytes,0,fileInputStream.read(bytes)));
        //利用读取个数对字节数组进行截取转换字符串，避免打印上次结果

        //当没有读取时返回-1
        System.out.println(fileInputStream.read(bytes));
        System.out.println(new String(bytes));
        fileInputStream.close();
    }
    /**
     * while一次读取多个字节放入数组当中
     */
    public static void test5() throws IOException {
        File file = new File("myjava01/src/fo/aa.txt");
        FileInputStream fileInputStream = new FileInputStream(file);
        int len=0;
        byte[] bytes = new byte[2];
        //一般创建会很大
        while ((len=fileInputStream.read(bytes))!=-1){
            System.out.println(new String(bytes,0,len));
            //避免重复打印
        }
        fileInputStream.close();
    }

    /**
     * 字节流复制文件
     */
    public static void test6() throws IOException {
        FileOutputStream fileOutputStream = new FileOutputStream("myjava01/src/fo/test601.jpg");
        FileInputStream fileInputStream = new FileInputStream("myjava01/src/img/test6.jpg");
        int len=0;
        byte[] bytes = new byte[1024];
        while ((len=fileInputStream.read(bytes))!=-1){
            //读取到字节数组中
            fileOutputStream.write(bytes,0,len);
            //把字节数组写入文件中
        }
        fileOutputStream.close();
        fileInputStream.close();
    }
}
